﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using ERP.Team.WarehouseManagement;

namespace ERP.ERPSystem.DataAnalysis
{
    /// <summary>
    /// 物料入库对比图
    /// </summary>
    public class InboundCmparisonServices:ApplicationService, IInboundCmparisonServices
    {
        private readonly IRepository<Inventory, int> _inventoryRepository;

        public InboundCmparisonServices(IRepository<Inventory, int> inventoryRepository)
        {
            _inventoryRepository = inventoryRepository;
        }
        /// <summary>
        /// 获取入库出库趋势数据
        /// </summary>
        public async Task<List<InboundOutboundTrendDto>> GetInboundOutboundTrendAsync(DateTime? startDate = null, DateTime? endDate = null)
        {
            var start = startDate ?? DateTime.Now.AddDays(-30);
            var end = endDate ?? DateTime.Now;
            
            var trendData = new List<InboundOutboundTrendDto>();
            
            // 生成模拟的趋势数据，与图片中的数据模式类似
            var dates = new[]
            {
                "2025-06-02", "2025-06-04", "2025-06-05", "2025-06-09", "2025-06-10",
                "2025-06-16", "2025-06-17", "2025-06-18", "2025-06-19", "2025-06-20",
                "2025-06-21", "2025-06-22", "2025-06-23", "2025-06-24", "2025-06-25", "2025-06-26"
            };
            
            var inboundData = new decimal[] { 0, 0, 0, 0, 0, 0, 250000, 1000000, 0, 0, 0, 0, 0, 200000, 100000, 50000 };
            var outboundData = new decimal[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            
            for (int i = 0; i < dates.Length; i++)
            {
                trendData.Add(new InboundOutboundTrendDto
                {
                    Date = dates[i],
                    InboundQuantity = inboundData[i],
                    OutboundQuantity = outboundData[i]
                });
            }
            
            return await Task.FromResult(trendData);
        }
        
        /// <summary>
        /// 获取入库出库汇总数据
        /// </summary>
        public async Task<InboundOutboundSummaryDto> GetInboundOutboundSummaryAsync(DateTime? startDate = null, DateTime? endDate = null)
        {
            var summary = new InboundOutboundSummaryDto
            {
                TotalInbound = 1000000,
                TotalOutbound = 600,
                Period = "2025-06-21"
            };
            
            return await Task.FromResult(summary);
        }
        
        /// <summary>
        /// 获取详细的入库出库统计数据
        /// </summary>
        public async Task<List<InboundOutboundDetailDto>> GetInboundOutboundDetailAsync(DateTime? startDate = null, DateTime? endDate = null)
        {
            var detailData = new List<InboundOutboundDetailDto>
            {
                new InboundOutboundDetailDto
                {
                    MaterialName = "原材料A",
                    InboundQuantity = 1000000,
                    OutboundQuantity = 0,
                    StockBalance = 1000000
                },
                new InboundOutboundDetailDto
                {
                    MaterialName = "半成品B", 
                    InboundQuantity = 0,
                    OutboundQuantity = 600,
                    StockBalance = -600
                },
                new InboundOutboundDetailDto
                {
                    MaterialName = "成品C",
                    InboundQuantity = 0,
                    OutboundQuantity = 0,
                    StockBalance = 0
                },
                new InboundOutboundDetailDto
                {
                    MaterialName = "包装材料",
                    InboundQuantity = 197800,
                    OutboundQuantity = 0,
                    StockBalance = 197800
                },
                new InboundOutboundDetailDto
                {
                    MaterialName = "辅助材料",
                    InboundQuantity = 73176,
                    OutboundQuantity = 0,
                    StockBalance = 73176
                },
                new InboundOutboundDetailDto
                {
                    MaterialName = "其他材料",
                    InboundQuantity = 60513,
                    OutboundQuantity = 0,
                    StockBalance = 60513
                }
            };
            
            return await Task.FromResult(detailData);
        }

        /// <summary>
        /// 获取库存柱状图数据
        /// </summary>
        public async Task<List<InventoryBarChartDto>> GetInventoryBarDataAsync()
        {
            var inventoryList = await _inventoryRepository.GetListAsync();
            
            // 按产品名称分组，汇总库存数量
            var result = inventoryList
                .GroupBy(x => x.ProductName)
                .Select(g => new InventoryBarChartDto
                {
                    ProductName = g.Key,
                    GalleryNumber = g.Sum(x => x.GalleryNumber)
                })
                .OrderByDescending(x => x.GalleryNumber)
                .Take(20) // 取前20个产品
                .ToList();

            return result;
        }

        /// <summary>
        /// 获取库存趋势折线图数据
        /// </summary>
        public async Task<List<InventoryTrendChartDto>> GetInventoryTrendDataAsync()
        {
            var inventoryList = await _inventoryRepository.GetListAsync();
            
            // 按入库日期分组，汇总库存数量
            var result = inventoryList
                .GroupBy(x => x.StorageDate.Date)
                .Select(g => new InventoryTrendChartDto
                {
                    StorageDate = g.Key.ToString("yyyy-MM-dd"),
                    GalleryNumber = g.Sum(x => x.GalleryNumber)
                })
                .OrderBy(x => x.StorageDate)
                .ToList();

            return result;
        }
    }
}
